﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XingFuStudio.Pioneer.Net;
using XingFuStudio.Pioneer.Entity;
using FluorineFx.IO;
using XingFuStudio.Pioneer.Container.Interface.Event;
using XingFuStudio.Pioneer.Applications.City.Config;
using XingFuStudio.Pioneer.Applications.City.Entity;
using XingFuStudio.Pioneer.Applications.City.Element;

namespace XingFuStudio.Pioneer.Applications.City.Operation {
    /// <summary>
    /// 操作基类
    /// </summary>
    public class OPBase {
        #region 事件

        /// <summary>
        /// 打印日志事件
        /// </summary>
        public event EventHandler<LogEventArgs> PrintLog = null;
        /// <summary>
        /// 出错事件
        /// </summary>
        public event EventHandler<XingFuStudio.Pioneer.Container.Interface.Event.ErrorEventArgs> Error = null;

        /// <summary>
        /// 执行打印日志事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="isNewLine"></param>
        /// <param name="formate"></param>
        /// <param name="args"></param>
        protected void DoPrintLog(bool isNewLine, string format, params object[] args) {
            if (this.PrintLog != null) {
                this.PrintLog(this, new LogEventArgs() {
                    IsNewLing = isNewLine, LogMessage = String.Format(format, args)
                });
            }
        }

        /// <summary>
        /// 执行出错事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="ex"></param>
        protected void DoError(Exception ex) {
            if (this.Error != null) {
                this.Error(this, new XingFuStudio.Pioneer.Container.Interface.Event.ErrorEventArgs() {
                    Exception = ex
                });
            }
        }

        #endregion

        /// <summary>
        /// 达到这个体力时，收获将增加体力
        /// </summary>
        protected int minEnergy = 2;

        /// <summary>
        /// Http请求管理类对象
        /// </summary>
        protected HttpManager httpManager = null;
        /// <summary>
        /// 帐户实体对象
        /// </summary>
        protected AccountInfo account = null;
        /// <summary>
        /// 我的城市信息
        /// </summary>
        protected CityInfo city = null;
        /// <summary>
        /// 用户配置
        /// </summary>
        protected UserConfigSection config = null;

        /// <summary>
        /// 创建一个实例
        /// </summary>
        public OPBase() {
        
        }

        /// <summary>
        /// 创建一个实例
        /// </summary>
        /// <param name="httpManager"></param>
        /// <param name="account"></param>
        /// <param name="city"></param>
        /// <param name="config"></param>
        public OPBase(HttpManager httpManager, AccountInfo account, CityInfo city, UserConfigSection config) {
            this.httpManager = httpManager;
            this.account = account;
            this.city = city;
            this.config = config;
        }

        /// <summary>
        /// 执行操作
        /// </summary>
        public virtual void Operate() {
            //
        }

        /// <summary>
        /// 更新城市信息
        /// </summary>
        /// <param name="exp"></param>
        /// <param name="cash"></param>
        /// <param name="goods"></param>
        /// <param name="energy"></param>
        protected void UpdateCityInfo(int exp, int cash, int goods, int energy) {
            this.city.Cash = cash;
            this.city.Exp = exp;
            this.city.Energy = energy;
            this.city.Goods = goods;

        }

        /// <summary>
        /// 获取配置（CCityConfig）相关的AMF信息
        /// </summary>
        /// <param name="target"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        protected AMFMessage GetAMFMessage(string target, params object[] args) {
            //将参数数组转换成列表
            List<object> objs = args.ToList();
            //添加三个格式固定的参数
            objs.Insert(0, target);
            objs.Insert(0, this.city.LoginId);
            objs.Insert(0, this.city.VerifyCode);
            //将列表转换成参数数组
            args = objs.ToArray();

            AMFMessage message = this.httpManager.GetAMFMessage(
                "http://www.kaixin001.com/city/gateway.php", target, "/1", args);

            return message;
        }

        /// <summary>
        /// 执行升级
        /// </summary>
        /// <param name="cityElemety">操作的元素</param>
        /// <param name="resultMessage">输出参数，用来返回结果信息</param>
        /// <returns></returns>
        public bool UpLevel(CityElement cityElemety, out string resultMessage) {
            //声明参数字典
            Dictionary<string, object> dicParam = new Dictionary<string, object>();

            List<object> element = new List<object>();
            List<object> lsElement = new List<object>();

            //Element参数字典
            Dictionary<string, object> dicElement = new Dictionary<string, object>();

            //赋值属性
            dicElement.Add("etype", 1);
            dicElement.Add("interval", 0);
            dicElement.Add("action", "uplevel");
            dicElement.Add("exData", new List<object>());
            dicElement.Add("orderid", cityElemety.OrderId);

            lsElement.Add(dicElement);
            element.Add(lsElement);

            dicParam.Add("element", element);
            dicParam.Add("task", new List<object>());

            //获取AMF数据
            AMFMessage message = this.GetAMFMessage("CCityAction.callfunc", "sync", dicParam);

            //解析数据
            Dictionary<string, object> content = (Dictionary<string, object>)message.Bodies[0].Content;
            Dictionary<string, object> data1 = (Dictionary<string, object>)content["data"];
            object[] element1 = (object[])data1["element"];
            Dictionary<string, object> result1 = (Dictionary<string, object>)element1[0];

            //获取升级是否成功的状态
            bool result = (string)result1["result"] == "succ";

            //如果成功，获取结果信息
            if (result) {
                resultMessage = String.Format("{0} 升级成功。{1}！", cityElemety.ElementName, (string)result1["msg"]);
                //更新城市信息
                this.UpdateCityInfo(city.Exp, city.Cash, city.Goods, city.Energy - 1);
            } else {
                resultMessage = String.Format("{0} 升级失败。{1}！", cityElemety.ElementName, (string)result1["msg"]);
            }

            return result;
        }
    }
}
